/**
 * Copyright (c) 2015-2019, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.controller.system;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.jfsa.manage.service.JfsaThemeService;
import cn.hg.jfsa.web.theme.ThemeFile;

/**
 * @author Golly
 */
public class JfsaThemeEditController extends BaseController {

	private Log log = Log.getLog(JfsaThemeController.class);
	@Inject
	private JfsaThemeService themeService;

	public void ajaxDelDir () {
		String dir = this.get("dir");
		String file = this.get("file");

		try {
			String msg = this.themeService.delDir(dir, file);

			if (StrKit.notBlank(msg)) {
				this.renderAjaxFailed("1", msg);
				return;
			}

		}
		catch (Exception e) {
			this.renderAjaxFailed("1", "文件夹删除失败！" + e.getMessage());
			this.log.warn("文件夹删除失败！", e);
			return;
		}

		this.renderAjaxSuccessed();
	}

	public void ajaxDelFile () {
		String dir = this.get("dir");
		String file = this.get("file");

		try {
			this.themeService.delFile(dir, file);
		}
		catch (Exception e) {
			this.renderAjaxFailed("1", "文件删除失败！" + e.getMessage());
			this.log.warn("文件删除失败!", e);
			return;
		}

		this.renderAjaxSuccessed();
	}

	public void ajaxLoadFileContent () {
		String dir = this.get("dir", "/");
		String file = this.get("file");

		try {
			String fileContent = this.themeService.getFileContent(dir, file);
			this.set("fileContent", fileContent);
			this.keepPara("file");
			this.renderAjaxSuccessed(Kv.by("fileContent", fileContent));
		}
		catch (Exception e) {
			this.renderAjaxFailed("500", "读取文件内容失败！");
			this.log.warn("读取文件内容失败！", e);
		}

	}

	public void ajaxNewDir () {
		String dir = this.get("dir");
		String file = this.get("file");

		try {
			this.themeService.newDir(dir, file);
		}
		catch (Exception e) {
			this.renderAjaxFailed("1", "文件夹创建失败！" + e.getMessage());
			this.log.warn("文件夹创建失败！", e);
			return;
		}

		this.renderAjaxSuccessed();
	}

	public void ajaxNewFile () {
		String dir = this.get("dir");
		String file = this.get("file");

		try {
			String msg = this.themeService.newFile(dir, file);

			if (StrKit.notBlank(msg)) {
				this.renderAjaxFailed("1", msg);
				return;
			}

		}
		catch (Exception e) {
			this.renderAjaxFailed("1", "文件创建失败！" + e.getMessage());
			this.log.warn("文件创建失败!", e);
			return;
		}

		this.renderAjaxSuccessed(Kv.by("file", file));
	}

	public void ajaxSaveFile () {
		String dir = this.get("dir");
		String file = this.get("file");
		String fileContent = this.get("fileContent");

		try {
			this.themeService.updateFileContent(dir, file, fileContent);
		}
		catch (Exception e) {
			this.renderAjaxFailed("1", "文件更新失败！" + e.getMessage());
			this.log.warn("主题文件更新失败!", e);
			return;
		}

		// 返回Ajax成功
		this.renderAjaxSuccessed();
	}

	@Override
	public void index () {
		String dir = this.get("dir", "/");
		String file = this.get("file");
		this.set("dir", dir);
		List<ThemeFile> fileList = this.themeService.getFileList(dir);
		this.set("fileList", fileList);

		if (StrKit.notBlank(file)) {
			this.set("file", file);
		} else {

			for (ThemeFile tf : fileList) {

				if (!tf.isDir()) {
					this.set("file", tf.getName());
					this.set("aceMode", this.themeService.getFileEditMode(tf.getName()));
					break;
				}

			}

		}

		if (!"/".equals(dir) && dir.length() > 1) {

			if (dir.lastIndexOf("/") == 0) {
				this.set("parentDir", "/");
			} else {
				String parentDir = dir.substring(0, dir.lastIndexOf("/"));
				this.set("parentDir", parentDir);
			}

		}

		if (StrKit.notBlank(file)) {
			this.set("aceMode", this.themeService.getFileEditMode(file));
		}

	}

}
